package com.fosss.band.service.impl;

import com.fosss.band.constant.FollowType;
import com.fosss.band.entity.Album;
import com.fosss.band.entity.Follow;
import com.fosss.band.entity.Song;
import com.fosss.band.entity.vo.SongVo;
import com.fosss.band.mapper.AlbumMapper;
import com.fosss.band.mapper.FollowMapper;
import com.fosss.band.mapper.SongMapper;
import com.fosss.band.service.SongService;
import com.fosss.band.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.fosss.band.utils.ResultCodeEnum.MESSAGE_UN_ENOUGH;

/**
 * @author: fosss
 * Date: 2023/12/27
 * Time: 20:41
 * Description:
 */
@Service
public class SongServiceImpl implements SongService {

    @Resource
    private SongMapper songMapper;
    @Resource
    private AlbumMapper albumMapper;
    @Resource
    private FollowMapper followMapper;

    /**
     * 根据专辑id查询歌曲
     */
    @Override
    public Result selectByAlbumId(Integer userId, Integer id) {
        List<Song> list = songMapper.selectByAlbumId(id);
        List<SongVo> collect = list.stream().map(song -> {
            SongVo songVo = new SongVo();
            BeanUtils.copyProperties(song, songVo);
            //查询该歌曲是否被关注
            Follow follow = followMapper.select(userId, FollowType.FOLLOW_TYPE_SONG, song.getId());
            songVo.setFollowed(follow != null);
            return songVo;
        }).collect(Collectors.toList());
        return Result.ok().data("rows", collect);
    }

    /**
     * 添加歌曲
     */
    @Override
    public Result add(Song song) {
        if (StringUtils.isEmpty(song.getName()) || StringUtils.isEmpty(song.getAuthor()) || song.getAlbumId() == null) {
            return Result.error().code(MESSAGE_UN_ENOUGH.getCode()).message(MESSAGE_UN_ENOUGH.getMessage());
        }
        songMapper.insert(song);
        return Result.ok();
    }

    /**
     * 删除歌曲
     */
    @Override
    public Result deleteById(Integer id) {
        songMapper.deleteById(id);
        return Result.ok();
    }

    /**
     * 根据id查询歌曲
     */
    @Override
    public Result getById(Integer id) {
        Song song = songMapper.getById(id);
        return Result.ok().data("row", song);
    }

    /**
     * 修改歌曲
     */
    @Override
    public Result update(Song song) {
        if (StringUtils.isEmpty(song.getName()) || StringUtils.isEmpty(song.getAuthor()) || song.getAlbumId() == null) {
            return Result.error().code(MESSAGE_UN_ENOUGH.getCode()).message(MESSAGE_UN_ENOUGH.getMessage());
        }
        songMapper.update(song);
        return Result.ok();
    }

    /**
     * 批量查询歌曲信息
     */
    @Override
    public List<SongVo> getSongVoByIds(List<Integer> ids) {
        List<Song> songList = songMapper.selectSongs(ids);
        List<SongVo> collect = songList.stream().map(song -> {
            SongVo songVo = new SongVo();
            BeanUtils.copyProperties(song, songVo);
            //查询专辑名
            Album album = albumMapper.getById(song.getAlbumId());
            songVo.setAlbumName(album.getName());
            return songVo;
        }).collect(Collectors.toList());

        return collect;
    }
}
